define([
    'jQuery',
    'Underscore',
    'Backbone',
    'Kendo',
    'i18n!nls/language',
    'views/tree/new',
    'text!templates/tree/new.html',
    'views/investigation/new',
    'text!templates/investigation/new.html'
], function($, 
            _, 
            Backbone, 
            Kendo, 
            i18n, 
            treeNewView, 
            treeNewTemplate, 
            investigationNewView, 
            investigationNewTemplate) {

    
    
    var registrationNewView = kendo.observable({
        
        
        
        // translations
        label: i18n.registration.view.new,
        
        // views
        investigation: null,
        tree: null,
        
        
        
        renderSubviews: function() {
            
            var templateTree = Kendo.template(treeNewTemplate);
            $("#tree_new").html(templateTree(treeNewView));
            
            treeNewView.renderSubviews();
            
            Kendo.bind($("#tree_new"), treeNewView);
            
            this.tree = treeNewView;
            
            
            var templateInvestigation = Kendo.template(investigationNewTemplate);
            $("#investigation_new").html(templateInvestigation(investigationNewView));
            
            investigationNewView.renderSubviews();
            
            Kendo.bind($("#investigation_new"), investigationNewView);
            
            this.investigation = investigationNewView;
            
        },
        
        
        
        save: function(e) {
            
            e.preventDefault();
            
//            alert('opslaan die handel!');
            
            var self = this;
            var deferredInvestigation = null;
            var deferredTree = this.tree.save();
            
            deferredTree.done(function(data) {
                
                var tree = data.models[0];
                
                console.log(tree);
                
                deferredInvestigation = self.investigation.save();
                
            });
            
        },
        
        
        
        // self executing function acts like a constructor (hence the name)
        __construct: (function(self, window, undefined) {
            
            // do nothing right now...
            
        })(this, window)
        
            
        
    });
    
    
    
    return registrationNewView;
    
    
    
});